package com.gitee.cirnochat.user.controller;

import com.gitee.cirnochat.common.annotation.AuthCheck;
import com.gitee.cirnochat.common.model.vo.resp.Result;
import com.gitee.cirnochat.common.utils.RequestUtils;
import com.gitee.cirnochat.user.model.vo.req.friend.FriendMarkRequest;
import com.gitee.cirnochat.user.model.vo.resp.FriendDetailVo;
import com.gitee.cirnochat.user.model.vo.resp.FriendVo;
import com.gitee.cirnochat.user.service.FriendService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.headers.Header;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "好友相关接口", description = "好友相关API")
@RestController
@RequestMapping("/friend")
@RequiredArgsConstructor
public class FriendInfoController {

    private final FriendService friendService;

    /**
     * 获取好友列表
     *
     * @param request HttpServletRequest
     * @return 好友列表
     */
    @GetMapping("/list")
    @AuthCheck
    @Operation(summary = "获取好友列表", description = "获取好友列表")
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = FriendVo.class)
            ),
            headers = {
                    @Header(name = "Authorization", description = "认证信息", required = true)
            }
    )
    @Parameter(name = "Authorization", description = "认证信息", required = true, in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    public Result<List<FriendVo>> listFriends(HttpServletRequest request) {
        Long userId = RequestUtils.getUserId(request);
        return Result.success(friendService.listFriends(userId));
    }


    /**
     * 获取好友详细
     * @param id 好友ID
     * @param request HttpServletRequest
     * @return 好友资料
     */
    @GetMapping("/get/{id}")
    @AuthCheck
    @Operation(summary = "获取好友信息", description = "获取好友信息")
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = FriendDetailVo.class)
            ),
            headers = {
                    @Header(name = "Authorization", description = "认证信息", required = true)
            }
    )
    @Parameter(name = "Authorization", description = "认证信息", required = true, in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    public Result<FriendDetailVo> getFriend(@PathVariable Long id , HttpServletRequest request) {
        Long userId = RequestUtils.getUserId(request);
        return Result.success(friendService.getFriendById(id,userId));
    }


    /**
     * 修改好友备注
     *
     * @param markRequest 好友备注请求
     * @param request     HttpServletRequest
     * @return 操作结果
     */
    @PutMapping("/remark")
    @AuthCheck
    @Operation(summary = "修改好友备注", description = "修改好友备注")
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)
            ),
            headers = {
                    @Header(name = "Authorization", description = "认证信息", required = true)
            }
    )
    @Parameter(name = "Authorization", description = "认证信息", required = true, in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    public Result<Void> updateRemark(@RequestBody FriendMarkRequest markRequest,
                                              HttpServletRequest request) {
        friendService.updateRemark(markRequest.getFriendId(), markRequest.getRemark(), request);
        return Result.success();
    }

    /**
     * 删除好友
     *
     * @param friendId 好友ID
     * @param request  HttpServletRequest
     * @return 操作结果
     */
    @DeleteMapping("/{friendId}")
    @AuthCheck
    @Operation(summary = "删除好友", description = "删除好友")
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)
            ),
            headers = {
                    @Header(name = "Authorization", description = "认证信息", required = true)
            }
    )
    @Parameter(name = "Authorization", description = "认证信息", required = true, in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    public Result<Void> deleteFriend(@Parameter(name = "friendId", description = "好友ID", required = true, in = ParameterIn.PATH) @PathVariable Long friendId,
                                     HttpServletRequest request) {
        friendService.deleteFriend(friendId, request);
        return Result.success();
    }

}
